package com.me.jingdongshop.model.protocol;

import android.os.Message;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created on 2017/5/4.
 * 网络请求基类
 * @author Chilam
 */

public class BaseProtocol {
    private IHttpService mHttpService;

    public IHttpService getService(){
        return mHttpService;
    }

    public BaseProtocol(){
        mHttpService = RetrofitManager.getInstance().getHttpService();
    }

    /**
     * 执行网络请求
     * @param call
     * @param callback
     * @param reqType 请求的接口类型
     * @param clazz JavaBean对象
     * @param what 区分不同网络请求（下拉刷新，上拉加载）
     */
    public <T> void execute(Call<JsonObject> call,
                            final HttpCallback callback,
                            final int reqType,
                            final Class<T> clazz,
                            final int what){

        call.enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                int code = response.code();
                String json = response.body().toString();
                //解析json数据
                try {
                    JSONObject jsonObject = new JSONObject(json);
                    if (code==200){//请求成功
                        Gson gson = new Gson();
                        T bean = gson.fromJson(json, clazz);

                        if(callback != null){
                            Message msg = new Message();
                            msg.obj = bean;
                            msg.what = what;
                            callback.onHttpSuccess(reqType, msg);
                            System.out.println();
                        }
                    }else{//请求失败    其他返回码的错误类型
                        String error = jsonObject.getString("error");
                        if (callback != null){
                            callback.onHttpError(reqType,error);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    if (callback != null) {
                        callback.onHttpError(reqType, e.getMessage());
                    }
                }
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                if (callback != null) {
                    callback.onHttpError(reqType, t.getMessage());
                    System.out.println();
                }
            }
        });


    }
    public <T> void execute(Call<JsonObject> call,
                            final HttpCallback callback,
                            final int reqType,
                            final Class<T> clazz) {
        execute(call, callback, reqType, clazz, 0);
    }
    /** 网络请求回调接口 */
    public interface HttpCallback{
        /**
         * 请求成功回调
         * @param reqType 请求的接口类型
         * @param msg 返回的JavaBean对象
         */
        public void onHttpSuccess(int reqType, Message msg);

        /**
         * 请求失败回调
         * @param reqType 请求的接口类型
         * @param error 失败原因
         */
        public void onHttpError(int reqType, String error);
    }
}
